<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\plan\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 脚本管理
 * @package app\script
 * @author LJQ<245369209@qq.com>
 */
class Script extends Api

{

    /**
   *  脚本列表
   * @param string 
   * @return Json
   */

	public function scriptList($planId=null,$scriptId=null){
              
             if (empty($planId)) {
                return json(['code'=>0,'msg'=>'没有找到预案']);
             }

             $where['reservePlanId']=['eq',$planId]; 
             if ($scriptId) {
                 $where['id']=['eq',$scriptId]; 
             }
             
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 脚本列表
			$info =collection(model('script')->with('unit')->where($where)->limit($startLimit.','.$limit)->order('stepIndex')->select())->toArray();
		}else{
			//脚本列表
			$info =collection(model('script')->with('unit')->where($where)->order('stepIndex')->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('script')->where($where)->count('id');   
		
    		      if ($info && $count == 0) {
         			return json(['code'=>0,'msg'=>'没有找到脚本']);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'脚本','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }

   	  
   	      //添加脚本
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('plan/script');
          $result = $model->change($data);
          if (false != $result) {
               setlog('scriptadd','script',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'脚本添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'脚本添加失败,请检查']);
          }
      }

   	

   /**
   * 修改脚本
   * @param  string scriptId 脚本id,
   * @return Json
   */
   	public function edit($data,$scriptId){
        
          if (empty($scriptId)) {
              return json(['code'=>0,'msg'=>'脚本不存在,请检查']);
          }
          $model = model('plan/script');     
          if ($model->change($data)) {
             setlog('scriptupdate','script',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'脚本修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'脚本脚本修改失败,请检查']);
            }
    }
    /**
   *  删除脚本
   * @param string scriptId 脚本id,
   * @param Json
   */
     
    public function del($scriptId) {
        $scriptId = input('scriptId',0,'intval');
      
        $model = model('plan/script');
      	if (empty ( $scriptId )||empty(($model->where(array('id'=>$scriptId))->find()))) {
           	return  json(['code'=>0,'msg'=>'脚本id不存在,请检查']);
        }
        if($model->destroy(['id'=>$scriptId]) !== FALSE){
              setlog('scriptdel','script',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'脚本删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'脚本删除失败,请检查']);
       }
    }


       /**
   *   获得企业列表
   * @return Json
   */
      public function getEnterprise() {
           $enterprise = model('basic/Enterprise');
           $enterprsies = collection($enterprise->field('id,fullName')->order('id')->select())->toArray();
           $enterprsies[] = array_unshift($enterprsies, ['id'=>0 , 'fullName'=>'管委会']);
           return json(['code'=>1,'msg'=>'企业列表','data'=>$enterprsies]);
    }

}
